package com.cloudcc.cloudframe.util;

import java.io.File;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.telephony.TelephonyManager;

/**
 * 应用程序 包管理器
 * 
 * @author wangzhiqiang
 * @date 2015-4-20 上午11:17:46
 * 
 */
public class PackageUtils {

	/**
	 * 判断一个包名 是否存在
	 * 
	 * @param pckName
	 * @param context
	 * @return
	 */
	public static boolean isPackageExist(String pckName, Context context) {
		try {
			PackageInfo pckInfo = context.getPackageManager().getPackageInfo(
					pckName, 0);
			if (pckInfo != null)
				return true;
		} catch (NameNotFoundException e) {
			// Logger.error(e.getMessage());
		}
		return false;
	}

	/**
	 * 获得包详情
	 * 
	 * @param pckName
	 *            包名称
	 * @param context
	 *            上下文
	 * @return PackageInfo
	 */
	public static PackageInfo getPackageInfo(String pckName, Context context) {
		try {
			return context.getPackageManager().getPackageInfo(pckName, 0);
		} catch (NameNotFoundException e) {
			// Logger.error(e.getMessage());
		}
		return null;
	}

	/**
	 * 获得版本号
	 * 
	 * @param packageName
	 * @param context
	 * @return
	 */
	public static int getVersionCode(String packageName, Context context) {
		int versionCode = 0;
		try {
			versionCode = context.getPackageManager().getPackageInfo(
					packageName, 0).versionCode;
		} catch (NameNotFoundException ex) {
			versionCode = 0;
		}
		return versionCode;
	}

	/**
	 * 获得版本号 默认获得 该app的版本号
	 * 
	 * @param context
	 * @return
	 */
	public static int getVersionCode(Context context) {
		return getVersionCode(context.getPackageName(), context);
	}

	/**
	 * 获得 VersionName
	 * 
	 * @param context
	 * @return
	 */
	public static String getVersionName(Context context) {
		String name = "";
		try {
			name = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0).versionName;
		} catch (NameNotFoundException ex) {
			name = "";
		}
		return name;
	}

	/**
	 * 安装apk
	 * 
	 * @param context
	 * @param file
	 */
	public static void installAPK(Context context, File file) {
		Intent intent = new Intent();
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.fromFile(file),
				"application/vnd.android.package-archive");
		context.startActivity(intent);
	}

	public static void uninstallApk(Context context, String packageName) {
		if (isPackageExist(packageName, context)) {
			Uri packageURI = Uri.parse("package:" + packageName);
			Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
					packageURI);
			context.startActivity(uninstallIntent);
		}
	}

	public static Intent getInstallApkIntent(File file) {
		Intent intent = new Intent();
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.fromFile(file),
				"application/vnd.android.package-archive");
		return intent;
	}

	/**
	 * 判断指定包名的进程是否运行
	 * 
	 * @param context
	 * @param packageName
	 *            指定包名
	 * @return 是否运行
	 */
	public static boolean isRunning(Context context, String packageName) {
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> infos = am.getRunningAppProcesses();
		for (RunningAppProcessInfo rapi : infos) {
			if (rapi.processName.equals(packageName))
				return true;
		}
		return false;
	}

	public static boolean isBackground(Context context, String packName) {
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> appProcesses = activityManager
				.getRunningAppProcesses();
		for (RunningAppProcessInfo appProcess : appProcesses) {
			if (appProcess.processName.equals(context.getPackageName())) {
				if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {// 后台
					return true;
				} else {
					return false;
				}
			}
		}
		return false;
	}

	/** 是否在前台运行
	 * @param context
	 * @param packName
	 * @return
	 */
	public static boolean isForeGround(Context context, String packName) {
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> appProcesses = activityManager
				.getRunningAppProcesses();
		for (RunningAppProcessInfo appProcess : appProcesses) {
			if (appProcess.processName.equals(context.getPackageName())
					&& appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 用来判断服务是否运行.
	 * 
	 * @param context
	 * @param className
	 *            判断的服务名字
	 * @return true 在运行 false 不在运行
	 */
	public static boolean isServiceRunning(Context mContext, String className) {
		boolean isRunning = false;
		ActivityManager activityManager = (ActivityManager) mContext
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningServiceInfo> serviceList = activityManager
				.getRunningServices(300);
		if (!(serviceList.size() > 0)) {
			return false;
		}
		for (int i = 0; i < serviceList.size(); i++) {
			if (serviceList.get(i).service.getClassName().equals(className)) {
				return true;
			}
		}
		return isRunning;
	}

	/**
	 * 启动一个app
	 * 
	 * @param context
	 * @param packageName
	 */
	public static void openApp(Context context, String packageName) {
		Intent mainIntent = context.getPackageManager()
				.getLaunchIntentForPackage(packageName);
		// mainIntent.setAction(packageName);
		if (mainIntent == null) {
			mainIntent = new Intent(packageName);
		}
		context.startActivity(mainIntent);
	}

	/**
	 * 获得手机的imei号码
	 * 
	 * @param context
	 * @return
	 */
	public static String getIMEI(Context context) {
		TelephonyManager tel = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return tel.getDeviceId();
	}

}
